package com.travel.api;

import com.travel.enums.OrderStates;
import com.travel.pojo.UserOrder;
import com.travel.service.MinioService;
import com.travel.service.UserOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.Base64;
/**
 * @Description: 获取签名图片
 * @Author: kohn
 * @CreateTime: 2024/7/11
 **/

@RestController
@RequestMapping("/api/signature")
@Api(tags = "签名图片接口")
public class SignatureController {
    @Autowired
    MinioService minioService;
    @Autowired
    UserOrderService userOrderService;
    private static final String SIGNATURE_MINIO_BUCKET = "signature-minio-bucket"; // 签名默认存储桶

    @ApiOperation("上传甲乙方签名")
    @ApiImplicitParam(value = "party",name = "a表示甲方，b表示乙方")
    @PostMapping("/{party:[a,b]}/upload") // a,甲方，b,乙方
    public void upload(@RequestParam("file") MultipartFile sigFile, @PathVariable("party") String party,
                         @RequestBody UserOrder userOrder) {
        if (!StringUtils.isEmpty(sigFile) && sigFile.getSize()>0){
            // 上传至minio签名桶，桶中的前缀为默认前缀
           String url = minioService.upload(sigFile, SIGNATURE_MINIO_BUCKET, "");
           if (party.equals("a")){
               userOrder.setASignmentImg(url);
           } else if (party.equals("b")) {
               userOrder.setBSignmentImg(url);
           }
           userOrderService.updateById(userOrder);
        }
    }
    @ApiOperation("根据订单id, 上传甲乙方签名")
    @ApiImplicitParam(value = "party",name = "a表示甲方，b表示乙方")
    @PostMapping("/{party:[a,b]}/upload/{orderId}") // a,甲方，b,乙方
    public void upload(@RequestParam("file") String data, @PathVariable("party") String party,
                       @PathVariable("orderId") String orderId) {
        // 1.获取到后面到数据
        // String data = "....";

        if (!data.startsWith("data:")) {
            throw new RuntimeException("无效的base64图片,请输入带前缀的base64");
        }
        String base64Data =  data.split(",")[1];
        // 2.解码成字节数组
        Base64.Decoder decoder = Base64.getMimeDecoder();
        byte[] bytes = decoder.decode(base64Data);
        // 3.字节流转文件
        InputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
        String url = minioService.uploadBytes(
                byteArrayInputStream,"image/png",SIGNATURE_MINIO_BUCKET,"/");

        UserOrder userOrder = userOrderService.getById(orderId);
        if (party.equals("a")){
            userOrder.setASignmentImg(url);
        } else if (party.equals("b")) {
            userOrder.setBSignmentImg(url);
        }
        userOrder.setState(OrderStates.TOBEPAY.getName());
        userOrderService.updateById(userOrder);

    }

    @ApiOperation("下载甲乙方签名")
    @ApiImplicitParam(value = "party",name = "a表示甲方，b表示乙方")
    @GetMapping("/{party:[a,b]}/download")
    public void download(@PathVariable("party") String party,
                         @RequestBody UserOrder userOrder,
                         HttpServletResponse response) {
        if (party.equals("a")){
            minioService.download(userOrder.getASignmentImg(), response);
        } else {
            minioService.download(userOrder.getBSignmentImg(), response);
        }
    }

}
